package com.nanxun.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanxun.domain.Member;
import com.nanxun.model.Result;
import com.nanxun.service.MemberService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 *后台管理系统维护会员控制层
 * @param
 * @return 
 * @author nanxun
 * @create 2025/6/20
 **/

@Api(tags = "后台管理系统会员接口管理")
@RequestMapping("admin/user")
@RestController
public class SysMemberController {

    @Resource
    private MemberService memberService;


    /**
     *
     * 多条件分页查询会员
     * @param current 页码
     * @param size 每页显示条数
     * @param nickName 会员昵称
     * @param status 会员状态
     * @return com.nanxun.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.nanxun.domain.Member>>
     * @author nanxun
     * @create 2025/6/20
     **/

    @ApiOperation("多条件分页查询会员")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:user:page')")
    public Result<Page<Member>> loadMemberPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) String nickName,
                                               @RequestParam(required=false) Integer status
                                               ){
        //创建会员分页对象
        Page<Member> page = new Page<>(current,size);
        //多条件分页查询会员
        page=memberService.page(page,new LambdaQueryWrapper<Member>()
                .eq(ObjectUtil.isNotNull(status),Member::getStatus,status)
                .like(StringUtils.hasText(nickName),Member::getNickName,nickName)
                .orderByDesc(Member::getCreateTime)
        );
        return Result.success(page);
    }

    
    
    /**
     *
     * 根据标识查询会员信息
     * @param id 会员标识id
     * @return com.nanxun.model.Result<com.nanxun.domain.Member>
     * @author nanxun
     * @create 2025/6/20
     **/
    
    //admin/user/info/5
    @ApiOperation("根据标识查询会员信息")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('admin:user:info')")
    public Result<Member> loadMemberInfo(@PathVariable Long id){
//Member member = memberService.getById(id);
//拿到啦会员所有信息，如果说我们现在只要会员的id,openId，头像，昵称，状态信息，需要用到其他方法getOne
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                //和写SQL一样，指定只查这几列
                .select(Member::getId, Member::getOpenId, Member::getPic, Member::getNickName,Member::getStatus)
                .eq(Member::getId, id)
        );
        return Result.success(member);
    }

    /**
     *
     * 修改会员状态
     * @param member 会员对象(id,status)
     * @return com.nanxun.model.Result<java.lang.String>
     * @author nanxun
     * @create 2025/6/20
     **/

    @ApiOperation("修改会员状态")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:user:update')")
    public Result<String> modifyMemberStatus(@RequestBody Member member){
        member.setUpdateTime(new Date());
        Boolean updated = memberService.updateById(member);
        return Result.handle(updated);
    }

    /**
     *
     * 批量删除会员
     * @param ids 会员id集合
     * @return com.nanxun.model.Result<java.lang.String>
     * @author nanxun
     * @create 2025/6/20
     **/
    @ApiOperation("批量删除会员")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:user:delete')")
    public Result<String> removeMember(@RequestBody List<Integer> ids){
       // boolean removed = memberService.removeBatchByIds(ids);//物理删除
        //创建会员对象集合
        List<Member> memberList=new ArrayList<>();
        //循环遍历会员id集合
        ids.forEach(id->{
            Member member = new Member();
            member.setId(id);
            member.setStatus(-1);//逻辑删除
            memberList.add(member);
        });
       boolean removed=memberService.updateBatchById(memberList);//逻辑删除
        return Result.handle(removed);
    }

    /// ////////////////////////feign接口////////////////////////
    @GetMapping("getNickNameByOpenId")
    public Result<String> getNickNameByOpenId(@RequestParam String openId){
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .select(Member::getNickName)
                .eq(Member::getOpenId, openId)
        );
        return Result.success(member.getNickName());
    }
}
